package com.xunji.standard.controller;

import com.xunji.standard.common.R;
import com.xunji.standard.entity.CaseFile;
import com.xunji.standard.entity.CaseUploadInfo;
import com.xunji.standard.service.CaseFileService;
import com.xunji.standard.service.CaseUploadInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName CaseUploadInfoController
 * @Description 上传号记录控制器
 * @Author xuqiang_imyours
 * @Date 2025/10/22 15:08
 * @Version 1.0
 */
@Slf4j
@Api(tags = "上传号记录")
@RestController
@RequestMapping("/cui")
public class CaseUploadInfoController {
    @Resource
    private CaseUploadInfoService caseUploadInfoService;
    @Resource
    CaseFileService caseFileService;

    @Value("${project_temp_path:}")
    private String projectTempPath;
    @Value("${project_temp_url:}")
    private String projectTempUrl;

    @GetMapping("/list/{caseId}")
    @ApiOperation(value = "上传号列表", notes = "上传号列表")
    public R list(@PathVariable String caseId) {
        List<CaseUploadInfo> list = caseUploadInfoService.getListByCaseId(caseId);
        return R.success(list);
    }

    @GetMapping("/file/info/list")
    @ApiOperation(value = "上传号文件列表", notes = "上传号文件列表")
    public R infoList(@RequestParam String caseId, @RequestParam Long infoId) {
        List<CaseFile> caseFiles = caseFileService.selectListByInfoIdAndCaseId(caseId, infoId);
        return R.success(caseFiles);
    }

    // 原始文件查看
    @GetMapping("/file/original/view")
    @ApiOperation(value = "原始文件查看", notes = "原始文件查看")
    public R originalView(@RequestParam String fileId) {
        CaseFile caseFile = caseFileService.getById(fileId);
        if (caseFile == null) return R.failure("文件不存在");
        String originalPath = caseFile.getOriginalPath();
        // 全路径截取相对路径
        String relativePath = originalPath.substring(projectTempPath.length());
        String url = projectTempUrl + relativePath;
        return R.success(url);
    }
}
